package com.compass.controller;

import com.compass.constant.Admin;
import com.compass.entity.Project;
import com.compass.entity.ProjectMemberDo;
import com.compass.entity.User;
import com.compass.mapper.ProjectMapper;
import com.compass.mapper.UserMapper;
import com.compass.service.ProjectService;
import com.compass.util.SessionUtil;
import com.compass.vo.LoginUser;
import com.compass.vo.ResponseMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: li
 * Date: 2019/3/31
 * Time: 16:39
 * PROJECT：compass
 * PACKAGE: com.compass
 *
 * @author:55324936@qq.com To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/project")
@Slf4j
public class ProjectController {

    @Autowired
    ProjectService projectService;
    @Autowired
    ProjectMapper projectMapper;
    @Autowired
    UserMapper userMapper;


    @RequestMapping("/projectPage")
    public ModelAndView projectPage(){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("/project/projectPage");
        return modelAndView;
    }

    @RequestMapping("/projectMemberPage")
    public ModelAndView projectMemberPage(Integer projectId){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("projectId",projectId);
        modelAndView.setViewName("/project/projectMemberPage");
        return modelAndView;
    }
    @RequestMapping("/addProject")
    public @ResponseBody ResponseMessage addProject(Project project){
        if(null ==project || StringUtils.isEmpty(project.getProjectName())){
            return ResponseMessage.errorResponse("项目名不能为空!");
        }
        ResponseMessage responseMessage = projectService.addProject(project);
        return responseMessage;
    }
    @RequestMapping("/addProjectMember")
    public @ResponseBody ResponseMessage addProjectMember(ProjectMemberDo projectMemberDo){
        if(null ==projectMemberDo || null ==projectMemberDo.getUserId()){
            return ResponseMessage.errorResponse("项目成员不能为空!");
        }
        ResponseMessage responseMessage = projectService.addProjectMember(projectMemberDo);
        return responseMessage;
    }

    @RequestMapping("/deleteProjectMember")
    public @ResponseBody ResponseMessage deleteProjectMember(ProjectMemberDo projectMemberDo){
        try {
            projectMapper.deleteProjectMember(projectMemberDo);
            return ResponseMessage.successResponse();
        }catch (Exception e){
            log.error("删除项目成员异常",e);
            return ResponseMessage.errorResponse("删除项目成员异常,"+Admin.ADMIN_MESSAGE);
        }
    }
    @RequestMapping("/getProject")
    public @ResponseBody
    Map getProject(Project project){
        ResponseMessage responseMessage = projectService.getProject(project);
        return (Map)responseMessage.getObj();
    }
    @RequestMapping("/getProjectMember")
    public @ResponseBody
    Map getProjectMember(ProjectMemberDo projectMemberDo){
        ResponseMessage responseMessage = projectService.getProjectMember(projectMemberDo);
        return (Map)responseMessage.getObj();
    }
    @RequestMapping("/switchProject")
    public ModelAndView switchProject(Integer projectId){
        LoginUser loginUser = SessionUtil.getSessionUser();
        loginUser.setLastProjectId(projectId);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("/index");
        User user =new User();
        user.setId(loginUser.getId());
        user.setLastProjectId(projectId);
        userMapper.modifyUser(user);
        //我们切换之后并没有修改数据库里面的用户的lastProjectId，那么意味着用户下次登录进来还会显示第一个项目
        return modelAndView;
    }
}
